package com.example.study.plus.mybatis_plus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.study.plus.mybatis_plus.entity.User;
import com.example.study.plus.mybatis_plus.mapper.UserMapper;
import com.example.study.plus.mybatis_plus.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 路飞4
 * @since 2023-04-15
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public User getUserByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", username);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public User getUserByUsername2(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","name","email","avatar");
        queryWrapper.eq("name", username);
        return baseMapper.selectOne(queryWrapper);
    }


    @Override
    public User getUserByUserId(Long userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", userId);
        return baseMapper.selectOne(queryWrapper);
    }
    @Override
    public User getUserByUserId2(Long userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","name","email","avatar");
        queryWrapper.eq("id", userId);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public int register(String name, String password, String email) {
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        user.setEmail(email);
        return baseMapper.insert(user);
    }

    //    @Cacheable(value = {"categroy"}, key = "#root.method.name")
    @Override
    public List<String> getUserList() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","name");
        List<User> users = baseMapper.selectList(queryWrapper);
        List<String> list = new ArrayList<>();
        for (User user : users) {
            list.add(user.getName());
        }
        log.info("数据库查询执行: " + list);
        return list;
    }

    @Override
    public int changePassword(String username, String newPassWord) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", username);
        User user = baseMapper.selectOne(queryWrapper);
        user.setPassword(newPassWord);
        return baseMapper.updateById(user);
    }

    @Override
    public List<User> fuzzySearchForUsers(long userId,String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","name","email","avatar");
        queryWrapper.like("name", username);
        queryWrapper.ne("id",userId);
        return baseMapper.selectList(queryWrapper);
    }
}
